﻿using log4net;
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using AnyOffice.Components;
using AnyOffice.general.knowledge;
namespace AnyOffice.general.knowledge
{
    public partial class readdisk : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
            Response.Clear();
            KnowledgeInfo knowledgeInfo = KnowledgeFactory.GetKnowledgeInfo();
            if ((knowledgeInfo != null) && (knowledgeInfo.CanRead || knowledgeInfo.CanEdit))
            {
                string str = (Request.QueryString["filename"] != null) ? Request.QueryString["filename"].ToString() : "";
                str = str.Replace("../", "").Replace("/", @"\");
                FileInfo info2 = new FileInfo(knowledgeInfo.DiskPath + str);
                if (info2.Exists)
                {
                    string str3 = ".doc.xls.ppt.docx.xlsx.pptx";
                    if (str3.IndexOf(info2.Extension.ToLower()) >= 0)
                    {
                        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(info2.Name, Encoding.UTF8));
                        string str4 = info2.Extension.ToLower();
                        if (str4 != null)
                        {
                            if (!(str4 == ".doc"))
                            {
                                if (str4 == ".xls")
                                {
                                    Response.ContentType = "application/vnd.ms-excel";
                                }
                                else if (str4 == ".ppt")
                                {
                                    Response.ContentType = "application/vnd.ms-powerpoint";
                                }
                                else if (str4 == ".docx")
                                {
                                    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                                }
                                else if (str4 == ".xlsx")
                                {
                                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                                }
                                else if (str4 == ".pptx")
                                {
                                    Response.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                                }
                            }
                            else
                            {
                                Response.ContentType = "application/msword";
                            }
                        }
                        int bufferSize = 10240;
                        FileStream stream = new FileStream(info2.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize);
                        try
                        {
                            byte[] buffer = new byte[bufferSize];
                            int count = 0;
                            int offset = 0;
                            while ((count = stream.Read(buffer, offset, buffer.Length)) > 0)
                            {
                                Response.OutputStream.Write(buffer, offset, count);
                            }
                        }
                        catch (Exception exception)
                        {
                            if (log.IsErrorEnabled)
                            {
                                log.Error("", exception);
                            }
                        }
                        finally
                        {
                            stream.Close();
                        }
                    }
                }
            }
        }
    }
}
